package org.takeback.chat.service;

import com.google.common.collect.ImmutableMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.takeback.chat.entity.GcLotteryDetail;
import org.takeback.chat.lottery.Lottery;
import org.takeback.chat.lottery.LotteryDetail;
import org.takeback.chat.store.room.Room;
import org.takeback.chat.store.room.RoomStore;
import org.takeback.chat.store.user.UserStore;
import org.takeback.chat.utils.NumberUtil;
import org.takeback.dao.BaseDAO;
import org.takeback.service.BaseService;
import org.takeback.util.BeanUtils;

@Service("game02Service")
public class Game02Service extends BaseService {
	@Autowired
	private RoomStore roomStore;
	@Autowired
	private UserStore userStore;
	@Autowired
	private GameMonitor monitor;

	@Transactional
	public void returnMasterLoteryMoney(Lottery lottery, double deposit) {
		Map<Integer, LotteryDetail> detail = lottery.getDetail();
		Iterator itr = detail.keySet().iterator();
		StringBuffer hql = new StringBuffer("update PubUser a set a.money = COALESCE(a.money,0)+:money where a.id in(");
		StringBuilder sb = new StringBuilder();
		while (itr.hasNext()) {
			Integer uid = (Integer) itr.next();
			sb.append(uid).append(",");
			this.dao.executeUpdate(
					"update GcLotteryDetail a set  a.addback =:addback,desc1='抢庄' where a.lotteryid = :lotteryid and a.uid =:uid",
					ImmutableMap.of("addback", Double.valueOf(deposit), "lotteryid", lottery.getId(), "uid", uid));
		}
		if (sb.length() > 0) {
			sb = sb.deleteCharAt(sb.length() - 1);
			hql = hql.append(sb).append(")");
			this.dao.executeUpdate(hql.toString(), ImmutableMap.of("money", Double.valueOf(deposit)));
		}
	}

	@Transactional(rollbackFor = { Throwable.class })
	public void saveDetail(Lottery lottery, LotteryDetail detail, double deposit, String gameType) {
		GcLotteryDetail gcLotteryDetail = (GcLotteryDetail) BeanUtils.map(detail, GcLotteryDetail.class);
		gcLotteryDetail.setLotteryid(lottery.getId());
		gcLotteryDetail.setGameType(gameType);
		gcLotteryDetail.setDeposit(deposit);
		gcLotteryDetail.setDesc1("牛" + NumberUtil.getPoint(detail.getCoin()));
		gcLotteryDetail.setRoomId(lottery.getRoomId());
		gcLotteryDetail.setMasterId(lottery.getSender().intValue());
		this.dao.save(GcLotteryDetail.class, gcLotteryDetail);
	}

	@Transactional(rollbackFor = { Throwable.class })
	public void changeMoney(Map<Integer, Double> data, Lottery lottery, double deposit, double masterDeposit,
			double water) {
		Room r = (Room) this.roomStore.get(lottery.getRoomId());
		for (Map.Entry<Integer, Double> en : data.entrySet()) {
			Integer uid = (Integer) en.getKey();
			Double v = (Double) en.getValue();
			Double exp = Double.valueOf(Math.abs(((Double) en.getValue()).doubleValue()));
			this.dao.executeUpdate(
					"update PubUser a set a.money =a.money + :money,a.exp=coalesce(a.exp,0)+:exp where a.id = :uid ",
					ImmutableMap.of("money", v, "exp", exp, "uid", uid));
			double myDeposit = deposit;
			if (uid.equals(lottery.getSender())) {
				myDeposit = masterDeposit;
			}
			Double inout = Double.valueOf(NumberUtil.round(v.doubleValue() - myDeposit));

			this.monitor.setData(lottery.getRoomId(), uid, inout);

			this.dao.executeUpdate(
					"update GcLotteryDetail a set  a.addback =:addback,a.inoutNum = :inoutNum where a.lotteryid = :lotteryid and a.uid =:uid",
					ImmutableMap.of("addback", v, "inoutNum", inout, "lotteryid", lottery.getId(), "uid", uid));
		}
		this.dao.executeUpdate(
				"update GcRoom a set a.sumFee =COALESCE(a.sumFee,0) + :water,sumPack = COALESCE(sumPack,0)+1 where a.id = :roomId ",
				ImmutableMap.of("water", Double.valueOf(water), "roomId", lottery.getRoomId()));
	}

	@Transactional(rollbackFor = { Throwable.class })
	public Double test() {
		List<Double> l = this.dao.findByHql("select sum(money)+(select sum(sumFee) from GcRoom ) from PubUser");
		return (Double) l.get(0);
	}

	@Transactional(rollbackFor = { Throwable.class })
	public void gameStop(Lottery lottery) {
		this.dao.executeUpdate("update GcRoom a set a.status=0 where id=:id",
				ImmutableMap.of("id", lottery.getRoomId()));
		this.dao.executeUpdate("update GcLottery a set a.status=2 where id=:id",
				ImmutableMap.of("id", lottery.getId()));
	}
}
